package cn.gemiman.daoyi.module.cms.controller.app.blogtalk;

import cn.gemiman.daoyi.framework.common.enums.CommonStatusEnum;
import cn.gemiman.daoyi.framework.common.enums.UserTypeEnum;
import cn.gemiman.daoyi.framework.common.pojo.CommonResult;
import cn.gemiman.daoyi.framework.common.pojo.PageResult;
import cn.gemiman.daoyi.framework.common.util.object.BeanUtils;
import cn.gemiman.daoyi.module.cms.controller.admin.blogtalk.vo.BlogTalkPageReqVO;
import cn.gemiman.daoyi.module.cms.controller.admin.blogtalk.vo.BlogTalkRespVO;
import cn.gemiman.daoyi.module.cms.dal.dataobject.appsitefootprint.AppSiteFootprintDO;
import cn.gemiman.daoyi.module.cms.dal.dataobject.blogtalk.BlogTalkDO;
import cn.gemiman.daoyi.module.cms.service.appsitefootprint.AppSiteFootprintService;
import cn.gemiman.daoyi.module.cms.service.blogtalk.BlogTalkService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import static cn.gemiman.daoyi.framework.common.pojo.CommonResult.success;
import static cn.gemiman.daoyi.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
import static cn.gemiman.daoyi.framework.web.core.util.WebFrameworkUtils.getAppId;

@Tag(name = "用户 APP - 说说")
@RestController
@RequestMapping("/cms/blog-talk")
@Validated
public class AppBlogTalkController {

    @Resource
    private BlogTalkService blogTalkService;
    @Resource
    private AppSiteFootprintService appSiteFootprintService;

    @GetMapping("/get")
    @Operation(summary = "获得说说")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<BlogTalkRespVO> getBlogTalk(@RequestParam("id") Long id) {
        BlogTalkDO blogTalk = blogTalkService.getBlogTalk(id);
        return success(BeanUtils.toBean(blogTalk, BlogTalkRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得说说分页")
    public CommonResult<PageResult<BlogTalkRespVO>> getBlogTalkPage(@Valid BlogTalkPageReqVO pageReqVO) {
        pageReqVO.setAppId(getAppId());
        pageReqVO.setStatus(CommonStatusEnum.ENABLE.getStatus());
        PageResult<BlogTalkDO> pageResult = blogTalkService.getBlogTalkPage(pageReqVO);
        PageResult<BlogTalkRespVO> bean = BeanUtils.toBean(pageResult, BlogTalkRespVO.class);
        appSiteFootprintService.generatorIsLike(UserTypeEnum.MEMBER.getValue(), getLoginUserId(), bean.getList(), AppSiteFootprintDO::getTalkId);
        return success(bean);
    }

}
